package com.hydra.wiki.config;

import org.neo4j.ogm.config.Configuration;
import org.neo4j.ogm.session.SessionFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.data.neo4j.repository.config.EnableNeo4jRepositories;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@org.springframework.context.annotation.Configuration
@EnableNeo4jRepositories(basePackages = "com.hydra.wiki.graph.repo")
@EnableTransactionManagement // 激活SDN隐式事务
public class Neo4JConfig {

    @Value("${neo4j.uri}")
    private String neo4jURI;

    @Value("${neo4j.username}")
    private String neo4jUsername;

    @Value("${neo4j.password}")
    private String neo4jPassword;

    @Bean
    public Configuration getConfiguration() {
        Configuration config =
                new Configuration();
        // TODO: Temporary uses the embedded driver. We need to switch to http
        // driver. Then we can horizontally scale neo4j
        config.driverConfiguration()
                //.setDriverClassName("org.neo4j.ogm.drivers.bolt.driver.BoltDriver")
                .setURI(neo4jURI)
                .setCredentials(neo4jUsername, neo4jPassword)
                .setConnectionPoolSize(150);
        return config;
    }

    @Bean("sessionFactory")
    public SessionFactory getSessionFactory(Configuration configuration) {
        // Return the session factory which also includes the persistent entities
        return new SessionFactory(configuration, "com.hydra.wiki.graph.entity");
    }
}
